import QtQuick 2.9
import QtQuick.Controls 2.5

Rectangle {
    id: root
    color: "#99333333"

    signal analyseSuccess()

    function show(boxName) {
        _connBoxName = boxName
        _tickCount = 0
        root.visible = true

        tmrTick.start()
    }

    //////////
    property string _connBoxName: ""
    property int _tickCount: 0 //模拟动画计数

    Timer {
        id: tmrTick
        interval: 400
        repeat: true
        running: false
        onTriggered: {
            if (_tickCount <= 1) {
                txtTitle.text = qsTr("連接設備")
                txtText.text = qsTr("正在連接設備……") + _connBoxName
            } else if (_tickCount == 2) {
                txtTitle.text = qsTr("接收數據")
            } else if (_tickCount <= 6) {
                txtText.text = qsTr("數據接收中……") + ((_tickCount - 3) * 25)
            } else if (_tickCount <= 9) {
                txtTitle.text = qsTr("分析")
                txtText.text = qsTr("分析中……")
            } else if (_tickCount >= 11) {
                analyseSuccess()
                tmrTick.stop()
                root.visible = false
                return;
            }

            _tickCount++;
        }

    }

    ///////////////////////////////////////////////

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.AllButtons
        hoverEnabled: true
        onClicked: {
            visible = true
        }
    }

    Rectangle {
        width: parent.width * 0.6
        height: parent.height * 0.4
        anchors.centerIn: parent

        color: "#f1f1f1"
        radius: 10

        Column {
            anchors.fill: parent
            Text {
                id: txtTitle
                width: parent.width
                height: parent.height * 0.2
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: height * 0.5
                text: qsTr("")
            }
            Rectangle {
                width: parent.width; height: 1
                color: "#999"
            }
            Text {
                id: txtText
                width: parent.width
                height: parent.height * 0.8
                font.pixelSize: txtTitle.height * 0.25
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                text: qsTr("")
            }

//            FlatButton {
//                width: parent.width
//                height: parent.height * 0.2
//                text: qsTr("確定")
//                textColor: "#2E75B6"
//                backgroundColor: "transparent"
//                onClicked: {
//                    root.visible = false
//                    clickedOK()
//                }
//            }
        }
    }
}
